<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.staticfile.org/vue/2.4.2/vue.js"></script>
    <style>
        .drag{
            position:absolute;
            background: red;
            opacity: 0.5;
            width: 200px;
            height: 200px;
           
        }
    </style>
</head>
<body>
    <div id="app">
        <p>注意要先给元素加上position定位属性，v-drag拖拽是通过更改top和left值来实现的</p>
        <div class="drag" v-drag></div>
    </div>
    <script>
        Vue.directive("drag",function(el){
            el.onmousedown=function(e){
                var strX=e.pageX-this.offsetLeft;
                var strY=e.pageY-this.offsetTop;
                document.onmousemove=function(e){
                    el.style.left=e.pageX-strX+"px";
                    el.style.top=e.pageY-strY+"px";
             
                };
                document.onmouseup=function(){
               document.onmousemove=document.onmouseup=null;
               
                }
            }
        });
        var vm=new Vue({
           el:'#app',
           data:{},
           methods:{}
        });
    </script>
</body>

</html>